package com.szy.security.auth.util.user.data;

import com.szy.security.auth.util.user.detail.AuthMenu;
import com.szy.security.auth.util.user.detail.AuthUser;
import com.szy.security.auth.util.user.detail.SzyUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhenyun.su
 * @comment : 获取认证数据，可公共
 * @since : 2020-10-16
 */
@Deprecated
public class SzyAuthService extends BaseAuthService {
    public SzyAuthService(){
    }

    public SzyUserDetails getUserDetails() {
        if (isAuthenticated() && getPrincipal() instanceof SzyUserDetails) {
            return (SzyUserDetails) getPrincipal();
        }
        return null;
    }

    public AuthUser getAuthUser() {
        SzyUserDetails userDetails = getUserDetails();
        if (userDetails != null) {
            return userDetails.getAuthUser();
        }
        return null;
    }

    public List<String> getAuthRole() {
        List<String> items = new ArrayList<>();
        SzyUserDetails userDetails = getUserDetails();
        if ((userDetails != null) && (!ObjectUtils.isEmpty(userDetails.getAuthRoles()))) {
            userDetails.getAuthRoles().forEach(authRole -> {
                items.add(authRole.getRole());
            });
        }
        return items;
    }

    public List<AuthMenu> getAuthMenu() {
        List<AuthMenu> items = new ArrayList<>();
        SzyUserDetails userDetails = getUserDetails();
        if ((userDetails != null) && (!ObjectUtils.isEmpty(userDetails.getAuthMenus()))) {
            userDetails.getAuthMenus().forEach(menu -> {
                items.add(menu);
            });
        }
        return items;
    }
}
